package com._520it.crm.web.controller;

import com._520it.crm.domain.UnderWriting;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.service.ICheckService;
import com._520it.crm.service.IOrderBillService;
import com._520it.crm.service.IUnderWritingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/underWriting")
public class UnderWritingController {

    @Autowired
    private IUnderWritingService underWritingService;
    @Autowired
    private IOrderBillService orderBillService;
    @Autowired
    private ICheckService checkService;


    @RequestMapping("")
    public String index() {
        return "underWriting";
    }

    @RequestMapping("/revise")
    public String revise(Long orderBillId,Long underWritingId, Model model){
        //把保单id 和 修改原因 带到批改页面
        model.addAttribute("orderBillId",orderBillId);
        model.addAttribute("underWritingId",underWritingId);
        return "reviseinfo";
    }

    @RequestMapping("/temp")
    @ResponseBody
    public AjaxResult temp(Long underWritingId,String comments){
        AjaxResult result = null;
        try {
            UnderWriting underWriting = new UnderWriting();
            underWriting.setId(underWritingId);
            underWriting.setComments(comments);
            underWritingService.updateByPrimaryKey(underWriting);
            result = new AjaxResult(true,"暂存成功");
        } catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,e.getMessage());
        }
        return result;
    }

    @RequestMapping("/pass")
    @ResponseBody
    public AjaxResult pass(Long underWritingId,long orderBillId , String param){
        AjaxResult result = null;
        try {
            underWritingService.updatePassStatus(underWritingId,param);
            orderBillService.updateStatusById(orderBillId,4);
            checkService.insertOrderBill(orderBillId);
            result = new AjaxResult(true,"核保成功");
        } catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,e.getMessage());
        }
        return result;
    }

    @RequestMapping("/notPass")
    @ResponseBody
    public AjaxResult notPass(Long underWritingId,long orderBillId , String param){
        AjaxResult result = null;
        try {
            underWritingService.updateNotPassStatus(underWritingId,param);
            orderBillService.updateStatusById(orderBillId,3);
            result = new AjaxResult(true,"退回成功");
        } catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,e.getMessage());
        }
        return result;
    }

    @RequestMapping("/querySuggestions")
    @ResponseBody
    public Map<String,String> querySuggestions(Long orderBillId){
        Map<String,String> suggestions = underWritingService.querySuggestions(orderBillId);
        return suggestions;
    }
}
